import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-02-09
 * Time: 12:10
 */
public class test1 {
    /**
     * LeetCode 56 合并区间
     */

    public int[][] merge(int[][] intervals) {
        // 如果数组为空，
        if(intervals.length==0){
            return new int[0][1];
        }
        Arrays.sort(intervals,(val1, val2) -> val1[0]-val2[0]);
        List<int[]> ret = new ArrayList<>();
        ret.add(intervals[0]);
        for(int i=0;i<intervals.length;i++){
            int l = intervals[i][0];
            int r = intervals[i][1];
            if(l<=ret.get(ret.size()-1)[1]){
                ret.get(ret.size() - 1)[1] = Math.max(ret.get(ret.size() - 1)[1],r);
            }else{
                ret.add(intervals[i]);
            }
        }
        return ret.toArray(new int[ret.size()][]);
    }
}
